package com.schoolcloud.serve.controller;

import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.common.model.R;
import com.schoolcloud.serve.dto.AttributeGroupAssociatedDTO;
import com.schoolcloud.serve.dto.GroupAttributeValuesDTO;
import com.schoolcloud.serve.entity.SkuAttrGroup;
import com.schoolcloud.serve.query.SkuAttrGroupQuery;
import com.schoolcloud.serve.service.AttrGroupAssociatedService;
import com.schoolcloud.serve.service.SkuAttrGroupService;
import com.schoolcloud.serve.service.SkuInteractionService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/serve/re-market/sku/attribute/group")
@RequiredArgsConstructor
public class SkuAttributeGroupController {
    private  final SkuAttrGroupService attrGroupService;
    private final AttrGroupAssociatedService attrGroupAssociatedService;
    private final SkuInteractionService skuInteractionService;
    @GetMapping("/page")
    public R<PageDTO<SkuAttrGroup>> getAttrGroupPage(SkuAttrGroupQuery query){
        PageDTO<SkuAttrGroup> page= attrGroupService.getAttrGroupPage(query);
        return R.ok(page);
    }
    @PostMapping
    public R createAttrGroup(@RequestBody SkuAttrGroup attrGroup){
        attrGroupService.save(attrGroup);
        return R.ok();
    }
    @PutMapping
    public R updateAttrGroup(@RequestBody SkuAttrGroup attrGroup){
        attrGroupService.updateById(attrGroup);
        return R.ok();
    }
    @DeleteMapping("/{id}")
    public R deleteAttrGroup(@PathVariable Long id){
        attrGroupService.removeById(id);
        return R.ok();
    }
    @GetMapping("/{id}")
    public R<SkuAttrGroup> getAttrGroup(@PathVariable Long id){
        SkuAttrGroup attrGroup= attrGroupService.getById(id);
        return R.ok(attrGroup);
    }
    @GetMapping("/by/category/{categoryId}")
    public R<List<SkuAttrGroup>> getAttrGroupByCategoryId(@PathVariable String categoryId){
        List<SkuAttrGroup> attrGroups= attrGroupService.getAttrGroupByCategoryId(categoryId);
        return R.ok(attrGroups);
    }
    @PostMapping("/associated")
    public R groupAssociated(@RequestBody @Validated AttributeGroupAssociatedDTO dto){
        attrGroupAssociatedService.groupAssociated(dto);
        return R.ok();
    }
    @GetMapping("/attribute/value/by/category/{categoryId}")
    public R<List<GroupAttributeValuesDTO>> getAttrGroupAndValuesByCategoryId(@PathVariable String categoryId){
        List<GroupAttributeValuesDTO> attrGroups= skuInteractionService.getAttrGroupAndValuesByCategoryIdAndSpuId(categoryId, null);
        return R.ok(attrGroups);
    }
}
